<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
		<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../../JS/common.js"></script>
		<script type="text/javascript" src="../../../JS/json2.js"></script>
	</head>

	<body>
		<div id="datazone">

		</div>
		<a href="#" class="easyui-linkbutton" style=" margin-left:40px;width:200px;" onclick="checkData()">查看数据</a>
		<div id="display" style="margin-top:20px;color: red;"></div>
		<script type="text/javascript">
			displayContent();
			var webData = new Object();

			function displayContent() {
				var content = "";
				var tmp = "";
				var _localData;
				for(i = 0; i < WebDataZoneList.length; i++) {
					t = WebDataZoneList[i].code;
					_localData = localStorage.getItem('webdata');
					if(_localData != null && _localData!="") {
						var s = {};
						s = JSON.parse(_localData);
						for(m in s) {
							if(m == t) {
								if(typeof(s[m].time) != "undefined") {
									WebDataZoneList[i].updatetime = s[m].time;
								} else {
									WebDataZoneList[i].updatetime = "";
								}
							}
						}
					} else {
						console.log("localStorage没有数据");
					}
				}
				for(var i = 0; i < WebDataZoneList.length; i++) {
					tmp = "<div style='margin-bottom:20px;margin-top:20px;'>";
					tmp = tmp + "<label>" + WebDataZoneList[i].city + "</label>";
					tmp = tmp + "<a href='#'  class='easyui-linkbutton' onclick=\"getWebData('" + WebDataZoneList[i].code + "','" + WebDataZoneList[i].city + "','new')\" style=' margin-left:40px;width:80px;'>最新数据</a>";
					tmp = tmp + "<a href='#'  class='easyui-linkbutton' onclick=\"getWebData('" + WebDataZoneList[i].code + "','" + WebDataZoneList[i].city + "','old')\" style=' margin-left:10px;width:80px;'>历史数据</a>";
					var m = "更新时间为" + WebDataZoneList[i].updatetime;
					tmp = tmp + "<div><label>" + m + "</label></div>";
					tmp = tmp + "</div>";
					content = content + tmp;
				}
				$("#datazone").append(content);
			}
			
			function replaceTimeFromat(_time){
				return _time.replace(/-/g, "/");
			}

			function getWebData(code, city, type) {
				var url = "";
				$("#display").html(city + "正在抓取");
				if(type == "old") {
					_url = RemoteLinkAddress + "/logistics/originalorder/getReviewWebOrder.do?WEBNAME=" + code;
				} else {
					_url = RemoteLinkAddress + "/logistics/originalorder/getWebOrder.do?WEBNAME=" + code;
				}

				$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
					type: "get",
					url: _url,
					dataType: "json", //服务器返回的数据类型
					success: function(result) {
						if(result != null) {
							var data = new Object();
							if(type=="old")
							{
								result=DelOldData(result);
							}
							for(var i = 0; i < result.length; i++) {
								result[i].DISTRIBUTION_DATE = replaceTimeFromat(result[i].DISTRIBUTION_DATE);
								result[i].ID = i;
								result[i].city = code;
								
							}
							console.log(result);
							data.rows = result;
							data.time = getNowFormatDate();
							data.total = result.length;
							var localValue = Object();
							var _localData = localStorage.getItem('webdata');
							if(_localData == null || _localData=="") {
								console.log("没有localstorge");
								localValue[code] = data;
							} else {
								localValue = JSON.parse(_localData);
								localValue[code] = data;
							}
							localStorage.setItem('webdata', JSON.stringify(localValue));
							$("#display").html(city + "抓取成功" + "抓取到" + result.length + "条数据");
						}
					}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
					error: function(xhr, status, error) {
						$.messager.alert("错误", "请求失败!");
					}
				});
			}

			function checkData() {
				AddTabIn("网络订单", parent.$('#tabs'), "UI/LOGISTICS/WebOrder/WebOrderAdd.html");
			}

			function DelOldData(res) //删除超期历史的数据
			{
				var currDate=getNowFormatDate2();
				var datediff;
				var _data=new Array();
				for(var i=0;i<res.length;i++)
				{
					 _data.push(res[i]);
				}
               return _data;
			}
		</script>
	</body>

</html>